//
//  YNaviController.swift
//  rxCoreLogic
//
//  Created by yq on 2021/1/26.
//

import UIKit

class YNaviController: UINavigationController, UINavigationControllerDelegate {
	
	var popDelegate: UIGestureRecognizerDelegate?

	override func viewDidLoad() {
        super.viewDidLoad()

		let barButtonItem = UIBarButtonItem.appearance()
		barButtonItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 14.0)], for: .normal)
		
		self.popDelegate = self.interactivePopGestureRecognizer?.delegate
		self.delegate = self
		
	}

	override var childForStatusBarStyle: UIViewController? {
		return topViewController
	}

	override func pushViewController(_ viewController: UIViewController, animated: Bool) {
		if viewControllers.count > 0 {
			viewController.hidesBottomBarWhenPushed = true
			let item = UIBarButtonItem(
				image: UIImage(named: "nav_back_black"),
				style: .done,
				target: self,
				action: #selector(popback)
			)
			viewController.navigationItem.leftBarButtonItem = item
		}
		super.pushViewController(viewController, animated: animated)
	}
	
	func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool){
		self.interactivePopGestureRecognizer?.delegate = (viewController == self.viewControllers[0]) ? self.popDelegate : nil
	}

}

extension YNaviController {
	@objc func popback() {
		popViewController(animated: true)
	}
}


/**
interactivePopGestureRecognizer
交互式返回手势识别器
*/
